import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  {
    name: 'product',
    path: '/product',
    component: Layout,
    redirect: '/product/category/list', // 自动重定向到默认子路由
    meta: { title: '商品管理',icon:'el-icon-shopping-cart-2' },
    children: [
      {
        name:'Trademark',
        path: 'trademark/list',
        component: () => import('@/views/product/trademark/list'),
        meta: { title: '品牌管理'}
      },
      {
        name:'Category',
        path: 'category/list',
        component: () => import('@/views/product/category/list'),
        meta: { title: '分类管理'}
      },
      {
        name:'Attr',
        path: 'attr/list',
        component: () => import('@/views/product/attr/list'),
        meta: { title: 'attr管理'}
      },
      {
        name:'Sku',
        path: 'sku/list',
        component: () => import('@/views/product/sku/list'),
        meta: { title: 'sku管理'}
      },
      {
        name:'Spu',
        path: 'spu/list',
        component: () => import('@/views/product/spu/list'),
        meta: { title: 'spu管理'}
      },
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
